<template>
	<view class="container">
		<web-view ref="myWebview" :src="wvSrc" @message="handleWebviewMessage"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wvSrc: "https://wisematch.com.cn",
				webviewStyle: {
					progress: {
						color: '#FF3333'
					}
				},
				permissions: {
					camera: false,
					recordAudio: false
				},
				platform: ""
			}
		},
		created() {
			const systemInfo = uni.getSystemInfoSync();
			this.platform = systemInfo.platform;
		},
		onLoad() {

		},
		onShow() {
			// #ifdef APP-PLUS
			// 
			this.sendPlatForm()
			this.handleCheckPermission()
			// #endif
		},
		watch: {
			// permissions: {
			// 	handler(newVal, oldVal) {
			// 		this.sendToWebview({
			// 			permissions: {
			// 				...newVal
			// 			},
			// 			platform: "APP"
			// 		})
			// 	},
			// 	deep: true
			// }
		},
		methods: {
			handleCheckPermission() {
				let camera = plus.navigator.checkPermission('android.permission.CAMERA') == 'authorized' ?
					true : false
				let recordAudio = plus.navigator.checkPermission('android.permission.RECORD_AUDIO') ==
					'authorized' ?
					true : false
				this.permissions = {
					camera,
					recordAudio
				}
				this.sendToWebview({
					permissions: {
						camera,
						recordAudio,
						time: new Date().getTime()
					}
				})
			},
			sendPlatForm() { //为兼容load事件无法触发问题
				const intervalId = setInterval(() => {
					let currentWebview
					try {
						currentWebview = this.$scope.$getAppWebview();
					} catch (error) {
						//TODO handle the exception
					}
					if (currentWebview) {
						this.sendToWebview({
							platform: "APP",
							system: this.platform
						})
						clearInterval(intervalId)
					}
				}, 500)

			},
			sendToWebview(data) {
				const currentWebview = this.$scope.$getAppWebview();
				const wv = currentWebview.children()[0];
				if (wv) {
					wv.evalJS(`window.receiveMessageFromApp(${JSON.stringify(data)})`);
				}
			},
			handleWebviewMessage(event) { // 处理 Webview 发回的消息
				console.log('收到 Webview 消息:', event.detail.data[0]);
				if (event.detail.data && event.detail.data[0]) {
					let eventData = event.detail.data[0]
					let {
						data,
						eventType
					} = eventData
					if (eventType == 'getPermission') {
						if (this.platform == 'android') {
							this.handleCheckPermission()
							if (!this.permissions.camera || !this.permissions.recordAudio) {
								this.getAndroidPermissions()
							}
						} else if (platform == 'ios') {

						}
					}
				}
			},
			getAndroidPermissions() {
				const permissions = [
					'android.permission.CAMERA',
					'android.permission.RECORD_AUDIO',
				];
				plus.android.requestPermissions(
					permissions,
					(e) => {
						this.handleCheckPermission()
					},
					(e) => {
						console.log('申请权限失败', e);
						uni.showModal({
							title: '权限申请失败',
							content: '应用需要摄像头和麦克风权限才能进行视频面试，请前往设置授予权限。',
							showCancel: false
						});
					}
				);
			},
			requestAndroidPermissions() {
				// 判断是否为Android平台
				if (plus.os.name.toLowerCase() !== 'android') {
					this.wvSrc = 'https://wisematch.com.cn'; //苹果会自动获取权限
					this.$nextTick(() => {
						this.sendToWebview({
							data: {
								platform: "APP"
							},

						})
					})
					return
				};
				const permissions = [
					'android.permission.CAMERA',
					'android.permission.RECORD_AUDIO',
				];
				plus.android.requestPermissions(
					permissions,
					(e) => {
						console.log('申请权限成功', e);
						// 权限申请成功后，再加载WebView
						// this.wvSrc = 'https://wisematch.com.cn'; // 替换为你的实际网址
						this.wvSrc = 'http://192.168.13.132:5173/#/wiser'; // 替换为你的实际网址
						// 接下来可以将权限状态传递给WebView（见第二步）
						this.$nextTick(() => {
							this.sendToWebview({
								data: {
									platform: "APP"
								},
							})
						})
						this.handleCheckPermission('camera')
						this.handleCheckPermission('record_audio')
					},
					(e) => {
						console.log('申请权限失败', e);
						// 处理权限被拒绝的情况，给用户一些提示
						uni.showModal({
							title: '权限申请失败',
							content: '应用需要摄像头和麦克风权限才能进行视频面试，请前往设置授予权限。',
							showCancel: false
						});
					}
				);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100%;
		padding-top: 50rpx;
	}
</style>